package com.jessehu.xutilstest;

import android.app.ProgressDialog;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.jessehu.xutilstest.utils.DebugLog;
import com.jessehu.xutilstest.utils.MyToast;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.File;

public class UploadTestActivity extends AppCompatActivity {

    @ViewInject(R.id.btn_upload_submit)
    private Button btnSubmit;

    private ProgressDialog mProgressDialog;
    private UploadTestActivity TAG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_test);
        x.view().inject(this);
        TAG = this;
    }

    @Event(R.id.btn_upload_submit)
    private void submit(View view) {
        uploadfile();
    }

    /**
     * 上传文件(支持多文件上传)
     */
    private void uploadfile() {
        //图片上传地址
        String url = "http://192.168.218.131:8080/Upload";
        RequestParams params = new RequestParams(url);
        params.setMultipart(true);// 有上传文件时使用multipart表单, 否则上传原始文件流.
        params.addParameter("file", new File(Environment.getExternalStorageDirectory() + "/1.mkv"));
//        params.addParameter("file",new File(getExternalCacheDir().getAbsolutePath()+"/1.apk"));
        x.http().post(params, new Callback.ProgressCallback<String>() {
            @Override
            public void onWaiting() {
                DebugLog.i("result", "上传等待");
                MyToast.showToast(TAG, "上传等待");
            }

            @Override
            public void onStarted() {
                uploadDialog();
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                mProgressDialog.setMax((int) total);
                mProgressDialog.setProgress((int) current);
            }

            @Override
            public void onSuccess(String result) {
                DebugLog.i("result", result);
                MyToast.showToast(TAG, "上传成功");
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                DebugLog.i("result", ex.toString());
                MyToast.showToast(TAG, "上传出错");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                DebugLog.i("result", cex.toString());
                MyToast.showToast(TAG, "取消上传");
            }

            @Override
            public void onFinished() {
                mProgressDialog.dismiss();
            }
        });

    }

    //创建进度条弹窗
    private void uploadDialog() {
        mProgressDialog = new ProgressDialog(TAG);
        mProgressDialog.setTitle("downloading");
        mProgressDialog.setCancelable(true);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCanceledOnTouchOutside(true);
        mProgressDialog.show();
    }
}
